package com.qinyh.controller;



import android.content.Context;
import android.util.Log;

import com.qinyh.entity.Book;
import com.qinyh.entity.Page;
import com.qinyh.entity.Shelf;
import com.qinyh.model.DataObtain;
import com.qinyh.util.DebugUtil;
import com.qinyh.util.TemplateStore;
import com.qinyh.util.TextSplitUtil;

/**
 * 翻页控制类
 * 
 * @author Administrator
 * 
 */
public class Controller {
	private DataObtain dataObtain = new DataObtain();
	@SuppressWarnings("unused")
	private Context ctt;
	public int width = 400;
	public int height = 600;

	public Controller(Context ctt) {
		this.ctt = ctt;
		dataObtain.setCtt(ctt);
	}

	/**
	 * 获得书架
	 * 
	 * @return
	 * @throws Exception
	 */
	public Shelf getShelf() throws Exception {
		Shelf shelf = dataObtain.getShelf();
		return shelf;
	}

	/**
	 * 获得当前页
	 * 
	 * @return
	 */
	public Page getCurrentPage() {
		Page page = TemplateStore.getCurrentPage();
		if (page == null) {
			initTemplatePage();
			page = TemplateStore.getCurrentPage();
		}
		return page;
	}

	/**
	 * 获得前一页
	 * 
	 * @return
	 */
	public Page getPrePage() {
		Page page = TemplateStore.getPrePage();
		if (page == null) {
			initTemplatePage();
			page = TemplateStore.getPrePage();
		}
		return page;
	}

	/**
	 * 获得下一页
	 * 
	 * @return
	 */
	public Page getNextPage() {
		Page page = TemplateStore.getNextPage();
		if (page == null) {
			initTemplatePage();
			page = TemplateStore.getNextPage();
		}
		return page;
	}

	/**
	 * 更新下一页
	 */
	public void updateNextPage() {
		try {
			// 阅读偏移量
			int startC = TemplateStore.getCurrentStart();
			int endC = TemplateStore.getCurrentEnd();
			int endN = TemplateStore.getNextEnd();
			int endNN = 0;
			Book book = TemplateStore.getCurrentBook();
			String content = book.getContent();
			Log.v("debug",
					"endC:" + endC + " contentLength:" + content.length());
			if (endC == content.length())
				return;
			TemplateStore.setPrePage(TemplateStore.getCurrentPage());
			TemplateStore.setCurrentPage(TemplateStore.getNextPage());
			endNN = endN
					+ TextSplitUtil.getSplitPageOffset(
							content.substring((int) endN, content.length()),
							width, height, TemplateStore.fontSize);
			Log.v("debug", "" + endN + "   " + endNN);
			Page next = new Page();
			next.setPageContent(content.substring((int) endN, (int) endNN));
			TemplateStore.setNextPage(next);
			TemplateStore.setPreStart(startC);
			TemplateStore.setCurrentStart(endC);
			TemplateStore.setCurrentEnd(endN);
			TemplateStore.setNextEnd(endNN);
		} catch (Exception e) {
			DebugUtil.debug(e);
		}
	}

	public void updateScrollPrePage() {
		try {
			// 阅读偏移量
			int startC = TemplateStore.getCurrentStart();
			if (startC == 0)
				return;
			int endC = TemplateStore.getCurrentEnd();
			int startP = TemplateStore.getPreStart();
			int startPP = 0;
			Book book = TemplateStore.getCurrentBook();
			String content = book.getContent();

			TemplateStore.setNextPage(TemplateStore.getCurrentPage());
			TemplateStore.setCurrentPage(TemplateStore.getPrePage());
			startPP = TextSplitUtil.getReverseSplitPageOffset(
					content.substring(0, startP), width, height,
					TemplateStore.fontSize);
			if (startPP == -1)
				startPP = 0;
			Page pre = new Page();
			pre.setPageContent(content.substring(startPP, startP));
			TemplateStore.setPrePage(pre);
			TemplateStore.setPreStart(startPP);
			TemplateStore.setCurrentStart(startP);
			TemplateStore.setCurrentEnd(startC);
			TemplateStore.setNextEnd(endC);
		} catch (Exception e) {
			DebugUtil.debug(e);
		}
	}

	/**
	 * 初始化当前页
	 */
	private void initTemplatePage() {
		try {
			// 获取当前在章的序号
			int chapterSeq = TemplateStore.getCurrentChapterSeq();
			// 阅读偏移量
			int startC = TemplateStore.getCurrentStart();
			int endC = TemplateStore.getCurrentEnd();
			int startP = 0;
			int endN = 0;
			Book book = TemplateStore.getCurrentBook();
			String conent = book.getContent();
			int[] offset = book.getOffsets();
			// 设置当前页
			if (startC == 0 && endC == 0) {
				startC = offset[chapterSeq];
				endC = startC
						+ TextSplitUtil.getSplitPageOffset(conent.substring(
								(int) startC, book.getContent().length()),
								width, height, TemplateStore.fontSize);
				Page current = new Page();
				current.setPageContent(conent.substring((int) startC,
						(int) endC));
				TemplateStore.setCurrentPage(current);
			} else {
				endC = startC
						+ TextSplitUtil.getSplitPageOffset(conent.substring(
								(int) startC, book.getContent().length()),
								width, height, TemplateStore.fontSize);
				Page current = new Page();
				current.setPageContent(conent.substring((int) startC,
						(int) endC));
				TemplateStore.setCurrentPage(current);
			}
			// 设置下一页
			if (endC != conent.length() - 1) {
				endN = endC
						+ TextSplitUtil.getSplitPageOffset(conent.substring(
								(int) endC, book.getContent().length()), width,
								height, TemplateStore.fontSize);
				Page next = new Page();
				next.setPageContent(conent.substring((int) endC, (int) endN));
				TemplateStore.setNextPage(next);

			}
			// 设置前一页
			if (startC != 0) {
				startP = TextSplitUtil.getReverseSplitPageOffset(
						conent.substring(0, (int) startC), width, height,
						TemplateStore.fontSize);
				Page pre = new Page();
				pre.setPageContent(conent.substring((int) startP, (int) startC));
				TemplateStore.setPrePage(pre);
			}
			TemplateStore.setPreStart(startP);
			TemplateStore.setCurrentStart((int) startC);
			TemplateStore.setCurrentEnd((int) endC);
			TemplateStore.setNextEnd(endN);
		} catch (Exception e) {
			DebugUtil.debug(e);
		}

	}
}
